<template>
  <div>
    <el-form labelWidth="120px" :disabled="status==1">
      <el-form-item label="合同种类" class="is-required">
        <el-select v-model="contracttype">
          <el-option label="业务合同" value="业务合同"></el-option>
          <el-option label="其他合同" value="其他合同"></el-option>
        </el-select>
        <div class="el-form-item__error" v-if="!$v.contracttype.required">请输入合同种类</div>
      </el-form-item>
      <el-form-item label="合同名称" class="is-required">
        <el-input v-model="contracttitle"></el-input>
        <div class="el-form-item__error" v-if="!$v.contracttitle.required">请输入合同名称</div>
      </el-form-item>
      <el-form-item label="合同编号" class="is-required">
        <el-input v-model="contractnumber"></el-input>
        <div class="el-form-item__error" v-if="!$v.contractnumber.required">请输入合同编号</div>
      </el-form-item>
      <el-form-item label="签约日期" class="is-required">
        <el-date-picker v-model="contractdate" type="date" placeholder="选择日期"></el-date-picker>
        <div class="el-form-item__error" v-if="!$v.contractdate.required">请选择签约日期</div>
      </el-form-item>
      <el-form-item label="合同总金额" class="is-required">
        <el-input v-model="contractamount"></el-input>
        <div class="el-form-item__error" v-if="!$v.contractamount.required">请输入合同总金额</div>
      </el-form-item>
      <el-form-item label="我方签约人" class="is-required">
        <el-input v-model="contractpeople"></el-input>
        <div class="el-form-item__error" v-if="!$v.contractpeople.required">请输入我方签约人</div>
      </el-form-item>
      <el-form-item label="备注">
        <el-input type="textarea" v-model="beizhu"></el-input>
      </el-form-item>
      <el-form-item label="合同简介" class="is-required">
        <el-input v-model="contractdes"></el-input>
        <div class="el-form-item__error" v-if="!$v.contractdes.required">请输入合同简介</div>
      </el-form-item>
      <el-form-item label="合同附件">
        <upload2 :filelimit="3" :filelink.sync="filearr1" :isedit="false" :ispic="false"></upload2>
        <!-- <el-upload action multiple ref="upload">
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>-->
      </el-form-item>
      <el-form-item label="选择审批流" v-if="status!='1'" class="is-required">
        <choosespl @getsplid="getsplid" :atypeid="'0107'" :num="num" v-if="ischoosespl"></choosespl>
        <div class="el-form-item__error" v-if="!$v.aid.required">请选择审批流</div>
      </el-form-item>
    </el-form>
    <div style="text-align:right" v-if="status==0">
      <el-button size="mini" @click="closedia">取 消</el-button>
      <el-button size="mini" type="primary" @click="submitleave">确 定</el-button>
    </div>
    <div style="text-align:right" v-if="status==2">
      <el-button size="mini" @click="closedia">取 消</el-button>
      <el-button size="mini" type="primary" @click="submitleave2">重新提交</el-button>
    </div>
  </div>
</template>
<script>
import upload2 from '~/components/common/upload2.vue';
import choosespl from '~/components/apply/choosespl.vue';
import { getStringDate } from '~/plugins/datetime.js';
import { required } from 'vuelidate/lib/validators';

export default {
  validations: {
    aid: {
      required,
    },
    contracttype: {
      required,
    },
    contracttitle: {
      required,
    },
    contractnumber: {
      required,
    },
    contractdate: {
      required,
    },
    contractamount: {
      required,
    },
    contractpeople: {
      required,
    },
    contractdes: {
      required,
    },
  },
  components: {
    upload2,
    choosespl,
  },
  props: {
    status: Number,
    checkdetail: Object,
    fun: Function,
  },
  data() {
    return {
      ts: this.newStore(),
      filearr1: [],
      aid: '',
      num: 0,
      ischoosespl: true,
      contractdes: '',
      beizhu: '',
      contracttitle: '',
      contracttype: '',
      contractnumber: '',
      contractdate: '',
      contractpeople: '',
      contractamount: '',
    };
  },
  mounted() {
    if (this.status != 0) {
      console.log(this.checkdetail, 'xxxxxxxxxxxxxxxxxxxxxxxxxx');
      this.aid = this.checkdetail.operation1[0].hash.aid;
      this.contractdes = this.checkdetail.operation1[0].hash.contractdes;
      this.beizhu = this.checkdetail.operation1[0].hash.beizhu;
      this.contracttitle = this.checkdetail.operation1[0].hash.contracttitle;
      this.contracttype = this.checkdetail.operation1[0].hash.contracttype;
      this.contractnumber = this.checkdetail.operation1[0].hash.contractnumber;
      this.contractdate = this.checkdetail.operation1[0].hash.contractdate;
      this.contractpeople = this.checkdetail.operation1[0].hash.contractpeople;
      this.contractamount = this.checkdetail.operation1[0].hash.contractamount;
      for (let i = 0; i < this.checkdetail.operation2.length; i += 1) {
        const a = {};
        a.url = this.checkdetail.operation2[i].hash.fileurl;
        a.name = this.checkdetail.operation2[i].hash.filename;
        this.filearr1.push(a);
      }
    }
  },
  methods: {
    getsplid(val) {
      this.aid = val;
    },
    closedia() {
      this.$emit('closedia', false);
      this.$router.go(-1);
    },
    submitleave() {
      if (this.$v.$invalid) {
        this.$message({
          type: 'error',
          message: ' 请仔细核对填写信息',
        });
      } else {
        this.$confirm('确认提交合同审批?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          const req = {
            handle:
                  '9000B8B8D347526960C5BAB1FB6894EF8B7B312373E9BEF47A8517CE4098C19A',
            atype: '0107',
            flowid: this.aid,
          };
          this.spost(this.ts, '/mainservlet', req).then((ts) => {
            const req1 = {
              handle:
                    '09D3B9E20F101E11CD5ED14F3DA05EF7E5F8A7980C727D360C5596A61637D2F64B73C71EED1C1BDA88D6D925C779BA4C',
              aid: ts.d.data.operation3[0].hash.approvalid,
              contractdes: this.contractdes,
              beizhu: this.beizhu,
              contracttitle: this.contracttitle,
              contracttype: this.contracttype,
              contractnumber: this.contractnumber,
              contractdate: getStringDate(this.contractdate),
              contractpeople: this.contractpeople,
              contractamount: this.contractamount,
              filename: this.postname(this.filearr1),
              fileurl: this.posturl(this.filearr1),
            };
            this.spost(this.ts, '/mainservlet', req1).then(() => {
              console.log(ts);
              this.$message({
                type: 'success',
                message: '发起合同审批成功！',
              });
              this.$emit('closedia', false);
            });
          });
        });
      }
    },
    submitleave2() {
      if (this.$v.$invalid) {
        this.$message({
          type: 'error',
          message: ' 请仔细核对填写信息',
        });
      } else {
        this.$confirm('确认提交合同审批?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          const req = {
            handle:
                  '9000B8B8D347526960C5BAB1FB6894EF17427C100F77D58DB382894F762E66D209652941AB86E5AD',
            atype: '0107',
            flowid: this.aid,
            approvalid: this.checkdetail.operation1[0].hash.aid,
          };
          this.spost(this.ts, '/mainservlet', req).then((ts) => {
            const req1 = {
              handle:
                    '09D3B9E20F101E11CD5ED14F3DA05EF7E5F8A7980C727D360C5596A61637D2F6DA686A0A023C8D3B54347431A207BA75',
              aid: ts.d.data.operation3[0].hash.approvalid,
              contractdes: this.contractdes,
              beizhu: this.beizhu,
              contracttitle: this.contracttitle,
              contracttype: this.contracttype,
              contractnumber: this.contractnumber,
              contractdate: getStringDate(this.contractdate),
              contractpeople: this.contractpeople,
              contractamount: this.contractamount,
              fileurl: this.posturl(this.filearr1),
              filename: this.postname(this.filearr1),
            };
            this.spost(this.ts, '/mainservlet', req1).then(() => {
              console.log(ts);
              this.$message({
                type: 'success',
                message: '重新提交成功！',
              });
              this.fun();
              // this.$emit('closedia', false);
            });
          });
        });
      }
    },
    posturl(arr) {
      const a = [];
      for (let i = 0; i < arr.length; i += 1) {
        a.push(arr[i].url);
      }
      return a;
    },
    // 公共方法图片数组转名称数组
    postname(arr) {
      const b = [];
      for (let i = 0; i < arr.length; i += 1) {
        b.push(arr[i].name);
      }
      return b;
    },
  },
};
</script>
<style scoped>
</style>
